/**
 * @author CT
 * @email 994599447@qq.com
 * @date 2017.8.14
 */

'use strict';

import React, { Component } from 'react';
import {
    StatusBar,
    InteractionManager,
    View,
} from 'react-native';
import { NavigationActions } from 'react-navigation'
import * as I18n from '../config/i18n'
import * as Storage from '../common/storage'
import SplashScreen from 'react-native-splash-screen'
import * as config from '../config/appConfig'
import Utils from '../common/utils'

/**
 * 程序入口之初始化页面
 */
export default class InitPage extends Component {
    constructor(props) {
        super(props);
    }

    componentWillMount(){
        this._initConfig();
    }

    componentDidMount() {
        //设置状态栏背景颜色
        // StatusBar.setBackgroundColor(Utils.shadeColor('#4598dc', -0.3), false);
        // alert(Utils.shadeColor('#00adb4', -0.3));
        //关闭启动页
        SplashScreen.hide();
        
        //是否是首次启动app
        Storage.load(config.StorageKey.IsNotFirstStartAPP)
        .then(data=>{
           if(data == true){
                this._intoResetPage('HomePage');

            }else{
                this._intoResetPage('WelcomePage');
            }
        });
        
    }
 
    //跳转页面，清除原有路由
    _intoResetPage = (pageName) => {
        const {navigator} = this.props;
        InteractionManager.runAfterInteractions(() => {
            //跳至首页
            //Reset方法会清除原来的路由记录，添加上新设置的路由信息
            const resetAction = NavigationActions.reset({
                index: 0,
                actions: [
                    NavigationActions.navigate({ routeName: pageName}),
                ]
            })
            this.props.navigation.dispatch(resetAction)
        });
    }

    //初始化配置信息
    _initConfig = () => {
        //持久化操作配置信息
        Storage.initStorage();
        //国际化配置信息
        I18n.initLocale();
    }

    render() {
        return (
            <View style={{flex: 1}}>
            </View>
        );
    }
 
}